package system

import (
	"cubeAdmin/common"
	"cubeAdmin/common/jwtauth"
	"cubeAdmin/controllers"
	"cubeAdmin/dto"
	"cubeAdmin/models"
	"cubeAdmin/vo"
	"encoding/json"
	"github.com/beego/beego/v2/core/logs"
	"github.com/beego/beego/v2/core/validation"
)

// DeptController 部门api
type DeptController struct {
	controllers.BaseController
}

func (c *DeptController) URLMapping() {
	c.Mapping("Post", c.Post)
	c.Mapping("GetAll", c.GetAll)
	c.Mapping("Put", c.Put)
	c.Mapping("Delete", c.Delete)
}

// Index 获取部门列表视图
// @router /index [get]
func (c *DeptController) Index() {
	c.Data["pageTitle"] = "部门列表"
	user, _ := jwtauth.GetAdminDetailUser(c.Ctx.Input)
	c.Data["user"] = user
	c.TplName = "system/dept/main.tpl"
}

// GetAll @Title 获取部门列表
// @Description 获取部门列表
// @Success 200 {object} controllers.Result
// @router / [get]
func (c *DeptController) GetAll() {
	name := c.GetString("name")
	enabled, _ := c.GetInt8("enabled", -1)
	list := models.GetDeptList(name, enabled)
	c.Ok(list)
}

// AllTree @Title 获取部门树
// @Description 获取部门树
// @Success 200 {object} controllers.Result
// @router /allTree [get]
func (c *DeptController) AllTree() {
	name := c.GetString("name")
	enabled, _ := c.GetInt8("enabled", -1)
	list := models.GetAllDepts(name, enabled)
	c.Ok(vo.ResultList{Content: list, TotalElements: 0})
}

// DTree 获取部门树
// @router /tree [get]
func (c *DeptController) DTree() {
	name := c.GetString("name")
	enabled, _ := c.GetInt8("enabled", -1)
	list := models.GetDeptList(name, enabled)
	c.DataTree(list)
}

// Add 部门添加页面
// @router /add [get]
func (c *DeptController) Add() {
	c.Data["title"] = "新增部门"
	c.TplName = "system/dept/add.tpl"
}

// Post @Title 添加部门
// @Description 添加部门
// @Success 200 {object} controllers.Result
// @router / [post]
func (c *DeptController) Post() {
	var model models.SysDept
	valid := validation.Validation{}
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &model)
	if err != nil {
		c.Fail(err.Error(), common.UnmarshalErr)
	}
	b, _ := valid.Valid(&model)
	if !b {
		for _, err := range valid.Errors {
			c.Fail(err.Message, common.ArgsValidErr)
		}
	}
	_, e := models.AddDept(&model)
	if e != nil {
		c.Fail(e.Error(), common.DatabaseOperationErr)
	}
	c.Ok("操作成功")
}

// Edit 跳转到部门编辑
// @router /edit [get]
func (c *DeptController) Edit() {
	uid, _ := c.GetInt64("id")
	dept, _ := models.GetDeptById(uid)
	c.Data["title"] = "部门修改"
	c.Data["dept"] = dept
	c.TplName = "system/dept/edit.tpl"
}

// Put @Title 修改部门
// @Description 修改部门
// @Success 200 {object} controllers.Result
// @router / [put]
func (c *DeptController) Put() {
	var model models.SysDept
	valid := validation.Validation{}
	json.Unmarshal(c.Ctx.Input.RequestBody, &model)
	b, _ := valid.Valid(&model)
	if !b {
		for _, err := range valid.Errors {
			c.Fail(err.Message, common.ArgsValidErr)
		}
	}
	e := models.UpdateByDept(&model)
	if e != nil {
		c.Fail(e.Error(), common.DatabaseOperationErr)
	}
	c.Ok("操作成功")
}

// UpdateByIdAndEnabled 更新启用状态
// @router /enabled [put]
func (c *DeptController) UpdateByIdAndEnabled() {
	var model dto.DeptEnabled
	json.Unmarshal(c.Ctx.Input.RequestBody, &model)
	e := models.UpdateDeptByIdAndEnabled(model.Id, model.Enabled)
	if e != nil {
		c.Fail(e.Error(), common.DatabaseOperationErr)
	}
	c.Ok("操作成功")
}

// Delete @Title 删除部门
// @Description 删除部门
// @Success 200 {object} controllers.Result
// @router / [delete]
func (c *DeptController) Delete() {
	var ids []int64
	json.Unmarshal(c.Ctx.Input.RequestBody, &ids)
	logs.Debug(ids)
	e := models.DelByDept(ids)
	if e != nil {
		c.Fail(e.Error(), common.DatabaseOperationErr)
	}
	c.Ok("操作成功")
}
